<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once('function/func.php');

class Profile extends MX_Controller {

	private $module = 'profile';
	private $table = 'profile';
	function __construct(){
		parent::__construct();
		$this->load->model($this->module.'_model','model');
		$this->load->model('admincp_modules/admincp_modules_model');
		$this->load->model('cart/cart_model','cart');
		$this->load->model('products/products_model','productsModel');
		if($this->uri->segment(1)=='admincp'){
			if($this->uri->segment(2)!='login'){
				if(!$this->session->userdata('userInfo')){
					header('Location: '.PATH_URL.'admincp/login');
					return false;
				}
				$get_module = $this->admincp_modules_model->check_modules($this->uri->segment(2));
				$this->session->set_userdata('ID_Module',$get_module[0]->id);
				$this->session->set_userdata('Name_Module',$get_module[0]->name);
			}
			$this->template->set_template('admin');
			$this->template->write('title','Admin Control Panel');
		}
	}
	/*------------------------------------ Admin Control Panel ------------------------------------*/
	public function admincp_index(){
		modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'r',0);
		$default_func = 'created';
		$default_sort = 'DESC';
		$data = array(
			'module'=>$this->module,
			'module_name'=>$this->session->userdata('Name_Module'),
			'default_func'=>$default_func,
			'default_sort'=>$default_sort
		);
		$this->template->write_view('content','BACKEND/index',$data);
		$this->template->render();
	}
	
	
	public function admincp_update($id=0){
		$this->load->model('Products_model');
		if($id==0){
			modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',0);
		}else{
			modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'r',0);
		}
		$result[0] = array();
		if($id!=0){
			$result = $this->model->getDetailManagement($id);
			$dataUnread = array(
				'status'=>1
			);
			modules::run('admincp/saveLog',$this->module,$id,'status','update',0,1);
			$this->db->where('id', $id);
			$this->db->update($this->table, $dataUnread);
		}
		$cate=$this->category->getCategory();
		$data = array(
			'result'=>$result[0],
			'module'=>$this->module,
			'id'=>$id,
			'cate'=>$cate,
		);
		$this->template->write_view('content','BACKEND/ajax_editContent',$data);
		$this->template->render();
	}
	
	public function admincp_save(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($_POST){
			//Upload Image
			$fileName = array('image'=>'');
			if($_FILES){
				foreach($fileName as $k=>$v){
					if(isset($_FILES['fileAdmincp']['error'][$k])){
						$typeFileImage = strtolower(substr($_FILES['fileAdmincp']['type'][$k],0,5));
						if($typeFileImage == 'image'){
							$tmp_name[$k] = $_FILES['fileAdmincp']["tmp_name"][$k];
							$file_name[$k] = $_FILES['fileAdmincp']['name'][$k];
							$ext = strtolower(substr($file_name[$k], -4, 4));
							if($ext=='jpeg'){
								$fileName[$k] = time().'_'.SEO(substr($file_name[$k],0,-5)).'.jpg';
							}else{
								$fileName[$k] = time().'_'.SEO(substr($file_name[$k],0,-4)).$ext;
							}
						}else{
							print 'Only upload Image.';
							exit;
						}
					}
				}
			}
			//End Upload Image
			
			if($this->model->saveManagement($fileName)){
				//Upload Image
				if($_FILES){
					foreach($fileName as $k=>$v){
						if(isset($_FILES['fileAdmincp']['error'][$k])){
							$upload_path = BASEFOLDER.IMAGES_PRODUCTS.$this->input->post('folderAdmincp').'/';
							move_uploaded_file($tmp_name[$k], $upload_path.$fileName[$k]);
						}
					}
				}
				//End Upload Image
				print 'success';
				exit;
			}
		}
	}
	
	public function admincp_delete(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'d',1);
		if($perm=='permission-denied'){
			print $perm;
			exit;
		}
		if($this->input->post('id')){
			$id = $this->input->post('id');
			$result = $this->model->getDetailManagement($id);
			$folderTmp=explode('/',$result[0]->image);
			modules::run('admincp/saveLog',$this->module,$id,'Delete','Delete');
			$this->db->where('id',$id);
			if($this->db->delete($this->table)){
				//Xóa hình khi Delete
				@unlink(BASEFOLDER.IMAGES_PRODUCTS.$folderTmp[0].'/'.$folderTmp[1]);
				return true;
			}
		}
	}
	
	public function admincp_ajaxLoadContent(){
		$this->load->library('AdminPagination');
		$config['total_rows'] = $this->model->getTotalsearchContent();
		$config['per_page'] = $this->input->post('per_page');
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchContent';
		$config['start'] = $this->input->post('start');
		$this->adminpagination->initialize($config);

		$result = $this->model->getsearchContent($config['per_page'],$this->input->post('start'));
		$data = array(
			'result'=>$result,
			'per_page'=>$this->input->post('per_page'),
			'start'=>$this->input->post('start'),
			'module'=>$this->module
		);
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->load->view('BACKEND/ajax_loadContent',$data);
	}

	
	public function admincp_ajaxSelect(){
		$idCate=$this->input->post('idCate');
		$folder=$this->category->getFolder($idCate);
		$showTags='';
		foreach($folder as $r){
			$showTags .='<option value="'.$r->folder.'">'.$r->folder.'</option>';
		}
		print $showTags;
	}
	
	public function admincp_ajaxUpdateStatus(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($perm=='permission-denied'){
			print '<script type="text/javascript">show_perm_denied()</script>';
			$status = $this->input->post('status');
			$data = array(
				'status'=>$status
			);
		}else{
			if($this->input->post('status')==0){
				$status = 1;
			}else{
				$status = 0;
			}
			$data = array(
				'status'=>$status
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('id'),'status','update',$this->input->post('status'),$status);
			$this->db->where('id', $this->input->post('id'));
			$this->db->update($this->table, $data);
		}
		
		$update = array(
			'status'=>$status,
			'id'=>$this->input->post('id'),
			'module'=>$this->module
		);
		$this->load->view('BACKEND/ajax_updateStatus',$update);
	}
	/*------------------------------------ End Admin Control Panel --------------------------------*/
	
	
	/*------------------------------------ FRONTEND ------------------------------------*/
	public function index(){	
		$data=array(
			'title' => 'Đăng nhập / Đăng ký',
			'module' => $this->module,
		);
		
		$this->load->view('FRONTEND/index',$data);
	}
	
	public function login(){
		if($_POST){
			$check=$this->model->checkLogin($this->input->post('email'));
			if($check[0]->passwork==md5($this->input->post('pass'))){
				$data = array(
				   'email' => $this->input->post('email'),
				   'id' => $check[0]->id,
				   'name' => $check[0]->name,
				   'brithday' => $check[0]->brithday,
				   'phone' => $check[0]->phone,
				   'address' => $check[0]->address,
				);
				$this->session->set_userdata('ProfileInfo',$data);
				print 1;
				exit;
			}else{
				print 0;
				exit;
			}
		}
	}
	
	public function loginCheckout(){
		$this->load->view('FRONTEND/logincheckout');
	}
	
	public function showlogin(){
		$this->load->view('FRONTEND/show_login');
	}
	
	public function logout(){
		$this->session->unset_userdata('ProfileInfo');
		header('Location: '.PATH_URL);
	}
	
	public function register(){	
		$this->load->helper('captcha');
		$strCap= rand_string(8);
		$cap=array(
			'word' => $strCap,
			'img_path' => './captcha/',
			'img_url' => PATH_URL.'./captcha/',
			'font_path' => './systems/fonts/arial.ttf',
			'img_width' => 152,
			'img_height' => 30,
			'expiration' => 50,
			'time' => time()
		);
		$img=create_captcha($cap);
		$cap_word=$cap['word'];
		
		$data=array(
			'title' => 'Đăng ký',
			'module' => $this->module,
			'images' => $img['image'],
			'cap_w'=> $cap_word,			
		);
		
		$this->load->view('FRONTEND/register',$data);
	}
	
	public function register_success(){		
		$data=array(
			'title' => 'Đăng ký thành công',
			'module' => $this->module,
		);
		
		$this->load->view('FRONTEND/success',$data);
	}
	
	public function formRegister(){	
		if($_POST){
			$checkUser=$this->model->checkUser($this->input->post('rEmail'));
			
			if($checkUser){
				print 'User';
				exit;	
			}else{
				$this->model->registerhome();
				if($this->input->post('checkout_register')){
					print 'continue';
					exit;
				}else{
					print 'success';
					exit;
				}
			}					
		}
	}
	
	public function info($id){
		$result=$this->model->getProfile($id);
		$data=array(
			'title' => 'Thông tin cá nhân',
			'module' => $this->module,
			'result' => $result,
		);
		
		$this->load->view('FRONTEND/info',$data);	
	}
	
	public function save_login(){
		if($_POST){					
			$this->model->save_login($this->input->post('idProfile'));
			print 'success';
			exit;		
		}
	}
	
	public function save_info(){
		if($_POST){					
			$this->model->save_info($this->input->post('idProfile'));
			print 'success';
			exit;		
		}
	}
	
	public function get_user(){
		$pass=$this->model->getPass($this->input->post('idProfile'));
		if($pass==md5($this->input->post('old_pass'))){
			print 'user';
			exit;
		}
	}
	
	public function check_Email(){
		$checkUser=$this->model->checkUser($this->input->post('rEmail'));
		if($checkUser){
			print 'User';
			exit;	
		}
	}
	
	public function ajaxLoad(){
		$this->load->library('AdminPagination');
		
		$config['total_rows'] = $this->cart->getTotal($this->input->post('idProfile'));
		$config['per_page'] = $this->input->post('per_page');
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchContent';
		$config['start'] = $this->input->post('start');
		$this->adminpagination->initialize($config);

		$result = $this->cart->getInfoCart($this->input->post('idProfile'),$config['per_page'],$config['start']);
		
		$data=array(
			'title' => 'Thông tin đơn hàng',
			'module' => $this->module,
			'result' => $result,
		);
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->load->view('FRONTEND/ajax_info_cart',$data);
	}
	
	public function payment($id){
		$this->load->library('AdminPagination');
		
		$config['total_rows'] = $this->cart->getTotal($id);
		$config['per_page'] = 10;
		$config['num_links'] = 3;
		$config['func_ajax'] = 'searchContent';
		$config['start'] = 0;
		$this->adminpagination->initialize($config);

		$result = $this->cart->getInfoCart($id,$config['per_page'],$config['start']);		
		
		$data=array(
			'title' => 'Thông tin đơn hàng',
			'module' => $this->module,
			'result' => $result,
		);
		$this->session->set_userdata('start',$this->input->post('start'));
		$this->load->view('FRONTEND/info_cart',$data);
	}
	
	public function payment_detail($id){
		$result=$this->cart->getCartDetail($id);
		$status=$this->cart->getCartStatus($id);
		$data=array(
			'title' => 'Thông tin đơn hàng',
			'module' => $this->module,
			'result' => $result,
			'status' => $status,
			'id' => $id,
		);
		$this->load->view('FRONTEND/info_cart_detail',$data);
	}
	
	public function forgot(){
		$data=array(
			'title' => 'Bạn quên mật khẩu',
			'module' => $this->module,
		);
		$this->load->view('FRONTEND/forgot',$data);
	}
	
	public function forgot_succes(){
		$result=$this->model->checkUser($this->input->post('email'));
		if($result){
			$pass=rand_num(6);
			$data=array(
				'passwork' => md5($pass),
			);
			$this->db->where('email',$this->input->post('email'));
			$this->db->update($this->table,$data);
			
			$from= 'children@gmail.com';
			$to= $this->input->post('email');
			$subject= 'Mật khẩu mới của bạn';
			$message= 'Mật khẩu mới của bạn là:'.$pass.'';
			$header= 'From: $fromrnReply-to: '.$from.'';
			if(mail($to, $subject, $message, $header)){
				print 'co';
			}				
		}else{
			print 'ko';	
		}
	}
	/*------------------------------------ End FRONTEND --------------------------------*/
}